<?php

//Must do: check times login
//Test https....
//CSRF
//Draw workflow....
class Index extends Public_Controller {

    public function __construct() {
        parent::__construct();

        $this->_load();
    }

    public function index() {
        $this->_replacePhrase();
    }
    
    public function ajax_get_auto_complete_data() {
        $key = $this->input->get('term');
        if($key == 'ap'){
        $availableTags = array(
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        );}
        else
        
        $availableTags = array(
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
        );
        
        echo json_encode($availableTags);
//        $this->return_success_json($availableTags);
    }
    public function ajax_get_auto_complete_data1() {
        $skey = $this->input->get('term');
        $adata = $this->tag->gets($skey);
        
        if(count($adata) < 5){
            $adata += $this->product->get_by_search_keyword($skey, 5 - count($adata));
        }
        
        echo json_encode($adata);
    }

    public function add() {
        if (!(bool) $this->input->is_ajax_request()) {
            redirect('product/admincp/index');
            return false;
        }

        $aConfig = $this->_getRuleProduct();
        $this->load->library('form_validation', null, 'validation');
        $this->validation->set_rules($aConfig);
        $this->validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->validation->run() === false) {
            $this->return_fail_json($this->_getValidationError());
            return false;
        }

        if ((bool) $aProduct = $this->input->post('product')) {
            if ((bool) $aProduct = $this->product->add($aProduct)) {
                $this->return_success_json($aProduct, $this->lang->line("phrase var='product.add_new_product_successfully'"));
                return true;
            }
        }

        return false;
    }

    public function getList($offset = null) {
        if (!(bool) $this->input->is_ajax_request()) {
            $this->_redirect_index();
        }

        $offset = !empty($offset) ? $offset : 1;
        $aSearch = $this->input->post('search');
        $aProduct = $this->product->getListAdmin($offset, $aSearch);

        //Get list of page.
        $aPage = $this->product->get_pager('product/admincp/index/getList');
        $aPage = $this->product->get_pager_admin($aPage);

        $aReturn = array(
            'list' => $aProduct,
            'pages' => $aPage
        );

        $this->return_success_json($aReturn, '');
    }

    public function delete() {
        if (!(bool) $this->input->is_ajax_request()) {
            $this->_redirect_index();
        }

        $aIds = $this->input->post('product_ids');
        $iCurPage = $this->input->post('cur_page');
        $aIds = (array) json_decode($aIds);

        if ((bool) $this->product->delete($aIds)) {
            $aResult = $this->product->getListAdmin((int) $iCurPage);
            //Prepare in case if delete all items in a page.
            if (!empty($aResult)) {
                $this->return_success_json(array('page' => $iCurPage), '');
            } else {
                $this->return_success_json(array('page' => $iCurPage - 1), '');
            }
            return true;
        }

        $this->return_fail_json(NULL, '');
    }

    public function get() {
        
    }

    public function edit() {
        if (!(bool) $this->input->is_ajax_request()) {
            redirect('product/admincp/index');
            return false;
        }

        $aConfig = $this->_getRuleProduct();
        $this->load->library('form_validation', null, 'validation');
        $this->validation->set_rules($aConfig);
        $this->validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->validation->run() === false) {
            $this->return_fail_json($this->_getValidationError());

            return false;
        }

        if ((bool) $aProduct = $this->input->post('product')) {
            //Get current page. Use when reload page current after edit.
            $iCurPage = $aProduct['cur_page'];
//            $iCurPage = $aProduct['cur_page'];
            if ((bool) $aProduct = $this->product->edit($aProduct)) {
                $aProduct['cur_page'] = $iCurPage;
                $this->return_success_json($aProduct, $this->lang->line("phrase var='product.edit_product_successfully'"));
                return true;
            }
        }

        return false;
    }

    private function _load() {
        $this->load->library('pagination');
        $this->load->helper(array('url', 'form'));
        $this->load->model('tag', 'tag', FALSE, 'tag');
        $this->load->model('product', 'product', FALSE, 'product');
        $this->lang->load('admin/general', $this->_slang_name);
        $this->lang->load('admin/product', $this->_slang_name);
    }

    private function _replacePhrase() {
        $this->_adata['token'] = (object) array("name" => $this->security->get_csrf_token_name(), "value" => $this->security->get_csrf_hash());
        //Get sort of product.
        $this->_adata['aSort'] = $this->product->getSort();
        $this->_adata['phrase_js'] = json_encode($this->lang->language);
        $this->fill_phrase('product', $this->_adata, $this->lang->language);
    }

}

?>